#pragma once
#include"comm.hpp"

//模拟客户端接收数据


//文件创建类
class lin
{
public:
    lin()
    {
        umask(0);
        int n = ::mkfifo(pipefile.c_str(),gmode);
        if(n < 0)
        {
            std::cout << "mkfifo创建文件失败" << std::endl;
            exit(1);
        }
        std::cout << "创建文件成功" << std::endl;
        sleep(5);
    }
    ~lin()
    {
        int n = ::unlink(pipefile.c_str());
        if(n < 0)
        {
            std::cout << "删除文件失败" << std::endl;
        }
        std::cout << "删除文件成功"<< std::endl;
    }
};

lin init;

class server
{
public:
    bool openfile()
    {
        int n = comm_openfile(_fp,O_RDONLY);
        if(n < 0)
        {
            return false;
        }
        return true;
    }


    //读
    int r_pipe(std::string* cur)
    {
        char buff[1024];
        int n = ::read(_fp,buff,sizeof(buff)-1);
        if(n > 0)
        {
            buff[n] = 0;
            *cur = buff;
        }
        return n;
    }
    
    //关闭文件
    void closefile()
    {
        comm_closefile(_fp);
    }

private:
    int _fp;
};